﻿namespace DynamicGeometry
{
    public partial class RootFigureList : CompositeFigure
    {
        public override void Recalculate()
        {
            var allFigures = this.GetAllFiguresRecursive();
            foreach (var figure in allFigures)
            {
                if (figure != this)
                {
                    figure.RecalculateAndUpdateVisual();
                }
            }
        }

        protected override void OnItemAdded(IFigure item)
        {
            item.RegisterWithDependencies();
            if (Drawing != null)
            {
                item.OnAddingToDrawing(Drawing);
                if (Drawing.Canvas != null)
                {
                    item.OnAddingToCanvas(Drawing.Canvas);
                    item.RecalculateAndUpdateVisual();
                }
            }
        }

        protected override void OnItemRemoved(IFigure item)
        {
            if (Drawing != null)
            {
                item.OnRemovingFromDrawing(Drawing);
                if (Drawing.Canvas != null)
                {
                    item.OnRemovingFromCanvas(Drawing.Canvas);
                }
            }
            item.UnregisterFromDependencies();
        }
    }
}
